﻿(function ($) {

    $.util.namespace("QA.Index");

    window.QA.Index.init = function () {

        var container = $("#qas");
        $.getJSON("/Common/question-answer.json", function (result) {
            var typeData = $.array.filter(result, function (item) { return item.show });
            var allQuestionAnswerData = [];
            $.each(typeData, function (index, item) {
                var li = $("<li />").attr({ id: item.QA + "-li" }).appendTo(container);
                li.text(item.Des);
                try {
                    $.getJSON("/QuestionAnswer/" + item.QA + ".json", function (result2) {
                        if ($.util.likeArrayNotString(result2)) {
                            var qaData = $.array.filter(result2, function (item3) { return item3.show });
                            allQuestionAnswerData.push({ QA: item, data: qaData });
                            var ul = $("<ul />").attr({ id: item.QA + "-li-ul", "class": "qs-q" }).appendTo(li);
                            $.each(result2, function (index2, item2) {
                                $("<li />").html("<a href=\"javascript:void(0);\" onclick=\"window.QA.showAnswer(this,'" + item2.answer + "','" + item2.target + "','" + item2.remarks + "');\" >" + item2.question + "</a>").appendTo(ul);
                            });
                        }
                    });
                }
                catch (ex) { }
            });
        });

    };

    window.QA.buildContent = function (answer, target, remarks) {
        var content = "";
        content += "<div style=\"padding:5px;width:450px;line-height:18px;\">";
        content += "<div style=\"color:#000;\">";
        content += answer;
        content += "</div>";
        content += " ";
        if (!$.string.isNullOrWhiteSpace(remarks)) {
            content += "<br />";
            content += "<div  style=\"color:#000;\">";
            content += remarks;
            content += "</div>";
        }
        if (!$.string.isNullOrWhiteSpace(target) && target != answer) {
            content += "<br />";
            content += "<div  style=\"color:#000;\">";
            content += "传送门：";
            content += "<a href=\"" + target + "\" target=\"_blank\">" + target + "</a>";
            content += "</div>";
        }

        content += " ";
        content += " ";
        content += "</div>";

        return content;
    };

    window.QA.showAnswer = function (thisArg, answer, target, remarks) {
        window.QA.clearAnswer();
        if ($.string.isNullOrWhiteSpace(answer)) { answer = "该问题尚无答案。"; }

        var state = $.data(thisArg, "tooltip");
        if (state) {
            $(thisArg).tooltip("destroy");
            return;
        }
        $(thisArg).tooltip({
            position: 'right',
            //showEvent: "click",
            hideEvent: "click",
            trackMouse: false,
            content: window.QA.buildContent(answer, target, remarks),
            onShow: function () {
                $(this).tooltip('tip').css({
                    backgroundColor: '#ccc',
                    borderColor: '#666'
                });
            },
            onHide: function () {
                $(this).tooltip("destroy");
            }
        }).tooltip("show");
    };

    window.QA.clearAnswer = function () {
        $.each($(".tooltip-f"), function (index, item) {
            if ($.data(this, "tooltip")) {
                $(this).tooltip("destroy");
            }
        });
    };

    $(function () {

        window.QA.Index.init();

        $(window.document).click(function (e) {
            var target = $(e.target);
            if (target.is(".tooltip-f") || target.parents(".tooltip").length > 0) { return; }
            var t = $(".tooltip-f");
            if (t.length && t.data("tooltip")) {
                t.tooltip("destroy");
            }
        });
    });

})(jQuery);